﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Cn.Edwon.Web.Support;
using System.Reflection;
using Cn.Cellula.Model;
using Cn.Cellula.Biz.Port;
using Cn.Edwon.Spring;
using Cn.Edwon.Web.Validation;
using Cn.Edwon.Web;
using Cn.Cellula.Biz;
using Cn.Edwon.Biz;
using Cn.Cellula.Model.Content;
using Cn.Cellula.Client;
namespace Cn.Cellula.Website.Admin.Content.UploadFile {
    public partial class Edit : PageSupport {
        String EOKey = null;
        IBiz biz = BizFactory.Get<IBiz>();
        protected void Page_Load(object sender, EventArgs e) {
            EOKey = Request.QueryString["id"];
            if (!Page.IsPostBack) {
                if (!String.IsNullOrEmpty(EOKey)) {
                    FillForm(EOKey);
                }
            }
        }

        void FillForm(String key) {
            TUploadFileEO eo = biz.Load<TUploadFileEO>(key,this.OwnerKey);
            this.txt_SrcName.Text = eo.SrcName;
            this.hid_OldPicUrl.Value = eo.FilePath;
        }

        protected void btn_save_Click(object sender, EventArgs e) {
            Boolean bk = QuickValidator.Execute(Request, this.txt_SrcName.Parent);
            if (!bk) {
                SiteInfo.Send(Page, TxtDef.ERROR, QuickValidator.Message);
                return;
            }
            if (String.IsNullOrEmpty(EOKey)) {
                try {
                    TUploadFileEO eo = new TUploadFileEO();
                    ClientBiz.UploadFile(ref eo,Request.Files[0], CodeTypeDef.Module.UploadFile, DateTime.Now.Year+"_"+DateTime.Now.Month, this.OwnerKey, this.Actor);
                    biz.Save<TUploadFileEO>(eo);
                    BizFactory.Get<ILogBiz>().LogInsert("上传文件", eo.SrcName, this.Actor, Request);
                } catch (Exception ex) {
                    SiteInfo.Send(Page, TxtDef.ERROR, ex.Message);
                    return;
                }
            } else {
                TUploadFileEO eo = biz.Load<TUploadFileEO>(EOKey,this.OwnerKey);
                if (!String.IsNullOrEmpty(this.file_uploador.Value)) {
                    ClientBiz.UploadFile(ref eo, Request.Files[0], CodeTypeDef.Module.UploadFile, DateTime.Now.Year + "_" + DateTime.Now.Month, this.OwnerKey, this.Actor);
                }
                eo.UploadDate = DateTime.Now;
                eo.SrcName = this.txt_SrcName.Text;
                biz.Update<TUploadFileEO>(eo);
                BizFactory.Get<ILogBiz>().LogUpdate("上传文件", eo.SrcName, this.Actor, Request);
            }
            SiteInfo.SendSucceed(Page, "List.aspx");
        }
    }
}
